/*
 * main.c
 *
 *  Created on: 2011-7-28
 *      Author: lizhi
 */
//#include<stdio.h>
#include"common.h"
#include"Init.h"
//#include "LCD/lcd_def.h"
///#include "CC1100/CC1100.h"
//#include "TEXT/font.h"
//#include "label.h"
//#include "string.h"
//#include "crc.h"
//#include "CC1100/wireless_comm.h"d
//#include "fontupd.h"
#include "type.h"
uint8_t data[2048];
uint8_t temp[512];  //��ʱ�ռ�
uint32_t ii,jj;
//uint32_t br;
  //FATFS fs;
  //FRESULT res;

  uint8_t buffer[512];
  uint8_t buffer2[512];

void Txt_Viewer(void);
void Pic_Viewer(void);
uint8_t Update_Font(void);
uint8_t Font_Init(void);

//const char filename[21][11]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp","6.bmp","7.bmp","8.bmp","9.bmp","10.bmp","11.bmp","12.bmp","13.bmp","14.bmp","15.bmp","16.bmp","17.bmp","18.bmp","19.bmp","20.bmp","21.bmp"};
//const char Txt_Name[5][11]={"1.txt","2.txt","3.txt","4.txt","5.txt"};

//uint8_t lizhi[]={"���ݿỪ���޹�˾Quaoartech  "};
extern void Init_Device(void);
//extern GPIO_InitTypeDef GPIO_InitStructure;
//extern u8 CC1100_TxRxBuf[1+CC1100_DATA_LEN ];
uint16_t crc_temp[3];
uint8_t init_state=0x58;
uint32_t sd_size=1;
uint32_t i,j;
uint16_t Cursorx1,Cursorx2,color_temp;

void Delay_us(uint32_t t)
{
	uint32_t i,j;
	for(i=0;i<t;i++)
	{
		j=10;
		while(j--)
		{
			__asm("NOP");	
		}
			
	}
}
void Delay_ms(uint32_t t)
{
	Delay_us(t*1000);
}
int main()
{

  Init_Device();
  //receive_NVIC_init();
//  ili9481_Initializtion();
//  ili9481_Clear(BLACK);  //����Ϊ**����
#if 1
//  SPI_Configuration();
//  if(SD_Init()==0)////���SD���ɹ�
//            {
//              __NOP();
//      ///lcd_display(130,60,"SD Card Checked OK ",16,0);
//            }
//    sd_size=SD_GetCapacity();
//
//    SPI_Initmn();

	for(;;)
	{
		i++;
		//if(i>50000)
		{
             //__delay_ms(1000);
			 Delay_ms(1000);
			i=0;
			blue=red;
			red=!red;
			//blue=red;
			//printf("test stm32gcc\r\n");
		}
	}
    //return 1;
}
    //CC1100_Init() ;
    //(void)SD_GetCSD(SD_csd);
    //(void)SD_GetCSDRegister(&sd_csd2);
#else
          SPI_Configuration();
          SPI_SetSpeed(SPI_SPEED_LOW);
	  SPI_Initmn();
	  CC1100_Init();
	  for(;;)
	    //CC1100_Init();
	    {
             CC1100_TxPacket();
             _delay_ms(1000);
             CC1100_TxRxBuf[0]=0x01;;
             CC1100_TxPacket();
             _delay_ms(1000);

	    }
#endif
//        buffer[0]=0xA1;
//        SD_WriteSingleBlock(font_base_addr+1,buffer);
        //Pic_Viewer();
	  //SPI_SetSpeed(SPI_SPEED_LOW);
	  //SPI_Initmn();
	  //CC1100_Init() ;
//	  __NOP();
//	  crc_temp[0]=udf_crc(lizhi,sizeof(lizhi),CRC_SEED);
//	  __NOP();
//	  crc_temp[1]=cal_crc(lizhi,sizeof(lizhi));
//	  __NOP();
//	  crc_temp[2]=cal_crc2(lizhi,sizeof(lizhi));
//	  __NOP();
//        i=0;
//        if(Font_Init()||(i==1))//�ֿⲻ����,������ֿ�
//        {
//                //lcd_display(60,70,"Font Updating...",16,0);
//                while(Update_Font()!=0)//������³���
//                {
//                        //lcd_display(60,90,"SYSTEM FILE LOST, Please Check....",16,0);
//                        _delay_ms(500);
//                }
//        }
//        ili9481_SetPoint(200,200,RED);
//        color_temp=ili9481_GetPoint(200,200);
//        //while(1);
//        point_color=YELLOW;
//        back_color=BLACK;
//
//        lcd_display(20,20,FrameInfo.Model,32,0);
//        lcd_display(52,20,FrameInfo.Marketprice,24,0);
//        lcd_display(76,20,FrameInfo.Memberprice,24,0);
//        lcd_display(100,20,FrameInfo.Products,24,0);
//
//        lcd_display(20,96+20,labelinfo.Model,32,0);
//        lcd_display(52,120+20,labelinfo.Marketprice,24,0);
//        lcd_display(76,120+20,labelinfo.Memberprice,24,0);
//        lcd_display(100,100+20,labelinfo.Products,24,0);
//
//        i=480;
//	  for(;;)
//	    {
//	        point_color=MAGENTA;back_color=BLACK;lcd_display(200,i,lizhi,16,1);
//	        point_color=CYAN;back_color=BLACK;lcd_display(216,i,lizhi,24,1);
//	        point_color=RED;back_color=BLACK;lcd_display(240,i,lizhi,32,1);
//	        i-=10;
//	        if(i<10)
//	          {
//	            uint16_t ix,iy;
//	            i=480;
//	            //ix=0;iy=200;
//	            for(ix=0;ix<480;ix++)
//	              for(iy=200;iy<280;iy++)
//	                {
//	                  ili9481_SetPoint(ix,iy,BLACK);
//	                }
//	          }
//	        //_delay_ms(10);
//
//	              CC1100_TxRxBuf[2]=0x08;
//	              CC1100_TxPacket();
//	              _delay_ms(500);
//
//	              //CC1100_TxPacket();
//	              //_delay_ms(1000);

//	    }
//}


/*
      SD_Init();
      //SD_ReadByte((font_base_addr+1+i)<<9,buffer,512);
      __NOP();
      SD_ReadSingleBlock(font_base_addr+1+i,buffer);
      __NOP();
      lcd_display(60,70,buffer);
      ++i;
      if(i==1442)
        {
          __NOP();
          lcd_display(120,70,"stoped");
        }

*/

#if    0
      while(1) //����GBK�ӿ�
      {
              fontcnt=0;
              for(i=0x81;i<0xff;i++)
              {
                      fontx[0]=i;
                      LCD_ShowNum(148,150,i,3,16);//��ʾ������ֽ�
                      for(j=0x40;j<0xfe;j++)
                      {
                              if(j==0x7f)continue;
                              fontcnt++;
                              LCD_ShowNum(148,170,j,3,16);//��ʾ������ֽ�
                              LCD_ShowNum(148,210,fontcnt,5,16);//��ʾ������ֽ�
                              fontx[1]=j;
                              Show_Font(148,190,fontx,16,0);
                              t=200;
                              while(t--)//��ʱ,ͬʱɨ�谴��
                              {
                                      delay_ms(1);
                                      key=KEY_Scan();
                                      if(key==1)goto UPD;
                              }
                              LED0=!LED0;
                      }
              }
      }
#endif


#if 0
  while(1)
    {
      //CC1100_TxPacket();
      //_delay_ms(1000);
      //CC1100_TxRxBuf[]={};
      //CC1100_TxPacket();
      //_delay_ms(1000);
      //strcpy(buffer)
//      strcpy(buffer,"zxy ");
      //SD_Init();
      __NOP();
      SD_WriteSingleBlock(300000,buffer);
      lcd_display(210,2,buffer);
      _delay_ms(200);
//      strcpy(buffer,"______________________________");
      lcd_display(190,2,buffer);
      _delay_ms(500);
	  lcd_display(170,2,buffer2);
      SD_ReadSingleBlock(font_base_addr+1+i,buffer2);
      if(buffer2[0]==0x00)
        SD_Init();
      i++;
	  //SD_ReadSingleBlock(300003,buffer2+20);

      lcd_display(170,2,buffer2);
      _delay_ms(200);
      __NOP();
#if 0
      CC1100_RxPacket();
      for(i=0;i<=CC1100_DATA_LEN;i++)
      {
        //USART_Put_Char(CC1100_TxRxBuf[i]) ;
        lcd_display_char(20, 8*i, CC1100_TxRxBuf[i]);
      }
      _delay_ms(500);
      CC1100_TxRxBuf[0]='A';
      CC1100_TxRxBuf[CC1100_DATA_LEN]=' ';
      CC1100_SetRxMode()   ;
#elif 1
      LED0=1;
      LED1=0;
      //_delay_ms(2000);
      __NOP();
      //Txt_Viewer();
      __NOP();
      Pic_Viewer();//ͼƬ���
      __NOP();
      LED0=0;
      LED1=1;
#endif
    }
#endif
    //return 0;
//}






//svoid Txt_Viewer(void)
//{
//  __NOP();
#if 0
    FIL file;
    u32 row=0,line=0;
//    unsigned char region=0;
//    unsigned char location=0;
//    unsigned long index=0;
    u16 xx=0;
    u8 Cnt=0,j=0;
     while(1)
     {
        if(j>4)
        {
          ili9481_Clear(BLUE);  //����Ϊ��ɫ����
          lcd_display(line9, 30, "TXT file complete...");
          _delay_ms(3000);     //��ʱ5��
          break;
        }
        res = f_mount(0, &fs);
        res = f_open(&file,Txt_Name[j++], FA_OPEN_EXISTING | FA_READ);   //��С˵
        if(res!=FR_OK)
         {
          //while(1);
           ili9481_Clear(BLUE);  //����Ϊ��ɫ����
          lcd_display(line5, 30, "TXT file read fail...");
          }
        _delay_ms(3000);     //��ʱ5��
        ili9481_Clear(Back_Ground);  //����Ϊ��ɫ����
        _delay_ms(2000);     //��ʱ5��

        TFTSetXY(0,0);
        row=0,line=0;
         for(;;)
         {
            res = f_read(&file, data, 1024, &br);   //��ȡ2048�����
            if (res || br == 0)
                break;   // error or eof
             for(xx=0;xx<br;xx++)
              {
                 if(data[xx]>=128)
                 {
                     if(xx>=1023)
                      {
                      lcd_display_char(row, line, ' ');//��߽����
//                      region   = data[1023]-0xa0;//�õ����  gb2312
                      res = f_read(&file, data, 1, &br);   //��ȡ2048�����
//                      location = data[0]-0xa0;//�õ�λ��
                      }
                      else
                      {
//                      region   = data[xx]-0xa0;//�õ����  gb2312
//                      location = data[xx+1]-0xa0;//�õ�λ��
                      }

                      //index = (94*(region-1)+location-1)*32;//�����ڵ�ַ
                      //LCD_Show_Chinese(row, line, index);

                      line+= 16;

                      xx++;


                    //lcd_set_cursor(row, line+16);//���ŵ���һ���ַ���ʼ��
                  }

                 else
                 {
                     if((data[xx]==0x0A)||(data[xx]==0x0D)||(data[xx+1]==0x0A)||(data[xx+1]==0x0D))
                     {
                          lcd_display_char(row, line, ' ');//��߽����
                          line=0;
                          if(Cnt<2)//���ƶ����϶���
                          {
                          row += 16;
                          Cnt++;
                          }

                     }
                     else
                     {
                     Cnt=0;
                     lcd_display_char(row, line, data[xx]);
                     line+= 8;

                     }
                  }

                  if(line>319)
                  {
                  row += 16;
                  line=0;
                  }
                  if(row>479)
                  {

                  row = 0;
                  _delay_ms(3000);     //��ʱ5��
                  //while(!GET_Key());
                  ili9481_Clear(Back_Ground);  //����Ϊ��ɫ����
                  }
                  if ((line > 304)&&(data[xx] > 128)&& (line!=320))//��ֹһ�е�������
                 {
                   lcd_display_char(row, line, ' ');//��߽����
                   line = 0 ;
                   row+=16;
                 }
             }

          }//end for(;;)
          f_close(&file);
              // Unregister work area prior to discard it
          f_mount(0, NULL);
       }//end while(1)
#endif
//}


//void Pic_Viewer(void)
//{
#if 0
    //u16 k;
    u16 i,j=0;
    FIL file;
    unsigned int cnt,file_cnt,temp1;
    u16 temp;
  while(1)
  {

          if(j>22)
          {
            lcd_display(line9, 30, "BMP PLAY ...");
            _delay_ms(300);     //��ʱ5��
            break;
          }

           for(j=1;j<=22;j++)
             {
                   //ili9481_Clear(YELLOW);  //����Ϊ��ɫ����
                   res = f_mount(0, &fs);
                   res = f_open(&file, filename[j], FA_OPEN_EXISTING | FA_READ);   //��ͼƬ
                   if(j>22)
                   {
                     break;
                   }
                    if(res!=FR_OK)
                   {
                        //while(1);
                        lcd_display(line5, 30, "BMP PLAY FAILD...");
                    }
                    res = f_read(&file, data, 0x36, &br);     //��ȡ�ļ�ͷ��Ϣ
                    TFTSetXY(0,0);
                    if(data[0]==0x42 && data[1]==0x4D)        //�ļ���ʽ���ΪBM
                        {
                                  //printp("\r\n ���Ե�3---ͨ��~��\r\n");

                                  temp = data[18]+(data[19]<<8)+(data[20]<<16)+(data[21]<<24);
                                  temp1 = data[22]+(data[23]<<8)+(data[24]<<16)+(data[25]<<24);
                                 // if(temp==240 && temp1==320)             //�ļ�����Ϊ��Width320*Height240
                                  if(temp==320 && temp1==480)             //�ļ�����Ϊ��Width320*Height240
                                  {

                                      temp = data[28]+(data[29]<<8);
                                        if(temp==32)                          //32λ��ǿ�����ɫ
                                        {
                                          file_cnt = data[2]+(data[3]<<8)+(data[4]<<16)+(data[5]<<24);//��ȡ�ļ�����
                                          cnt=36;
                                              while(1)
                                              {
                                                res = f_read(&file, data, 2048, &br);   //��ȡ2048�����
                                                cnt += sizeof(data);
                                                if(cnt >= file_cnt)                             //���ȳ����ļ�����,�˳�
                                                  break;

                                                        for(i=0;i<512;i++)                              //��ʾ���
                                                        {
                                                          ili9481_WriteData((data[i*4+2]>>3)<<11 |
                                                                       (data[i*4+1]>>2)<<5 |(data[i*4]>>3));

                                                        }


                                                    }

                                               }
                                        else if(temp==24)                     //24λ���ɫ
                                        {
                                            file_cnt = data[2]+(data[3]<<8)+(data[4]<<16)+(data[5]<<24);  //��ȡ�ļ�����
                                            cnt=36;
                                            while(1)
                                            {
                                              res = f_read(&file, data, 1536, &br);                     //��ȡ1536�����
                                              if(res!=FR_OK)
                                             {
                                                  //while(1);
                                                  lcd_display(line5, 30, "2BMP PLAY FAILD...");
                                                  _delay_ms(3000);
                                              }
                                              cnt += 1536;
                                              if(cnt >= file_cnt)                                       //���ȳ����ļ�����,�˳�
                                                break;
                                              for(i=0;i<512;i++)
                                              {
                                                ili9481_WriteData((data[i*3+2]*32/256)<<11 |
                                                              (data[i*3+1]*64/256)<<5 |(data[i*3]*32/256));

                                              }
                                            }
                                        }//end ese if

                                         else if(temp==16)                     //24λ���ɫ
                                        {
                                            file_cnt = data[2]+(data[3]<<8)+(data[4]<<16)+(data[5]<<24);  //��ȡ�ļ�����
                                            cnt=36;
                                            while(1)
                                            {
                                              res = f_read(&file, data,1024, &br);                     //��ȡ1536�����
                                              cnt += 1024;
                                              if(cnt >= file_cnt)                                       //���ȳ����ļ�����,�˳�
                                                break;
                                              for(i=0;i<512;i++)
                                              {

                                               ili9481_WriteData(((data[i*2+1])<<9|(data[i*2])));

                                              }
                                            }
                                        }//end ese if

                                  }
                                  else
                                  {
                                     ili9481_Clear(WHITE);  //����Ϊ��ɫ����
                                      //delay_ms(500);     //��ʱ3��
                                      //d_WriteString(50,140," Image is too big... ",RED,YELLOW);
                                      //Lcd_WriteString(50,160,"  Play  Images~!   ",RED,YELLOW);
                                      lcd_display(line5, 30, "BMP ERROR...");
                                      _delay_ms(500);     //��ʱ3��
                                      ili9481_Clear(WHITE);  //����Ϊ��ɫ����
                                      TFTSetXY(0,0);
                                      file_cnt = data[2]+(data[3]<<8)+(data[4]<<16)+(data[5]<<24);//��ȡ�ļ�����
                                          cnt=36;
                                              while(1)
                                              {
                                                res = f_read(&file, data, 2048, &br);   //��ȡ2048�����
                                                cnt += sizeof(data);
                                                if(cnt >= file_cnt)                             //���ȳ����ļ�����,�˳�
                                                  break;

                                                    for(i=0;i<512;i++)                              //��ʾ���
                                                    {
                                                      ili9481_WriteData((data[i*4+2]>>3)<<11 |
                                                      (data[i*4+1]>>2)<<5 |(data[i*4]>>3));
                                                    }

                                               }
                                   }//end else
                        }//end if
                   // Close open files
              f_close(&file);
              // Unregister work area prior to discard it
              f_mount(0, NULL);
              _delay_ms(200); //_delay_ms(2000);     //��ʱ3��
              }//end for(;;)

       }//end while(1)
#endif
//}
